package com.gitee.helioz.openai.api.endpoint.completions;

import com.gitee.helioz.openai.api.OpenAiService;
import com.gitee.helioz.openai.api.dto.completions.CompletionResponse;
import com.gitee.helioz.openai.api.dto.completions.CompletionCreateRequest;
import com.gitee.helioz.openai.common.constants.OpenAiApiUrl;
import com.gitee.helioz.openai.common.util.json.GsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 〈文本补全服务〉
 *
 * @author <a href="https://gitee.com/helioz">Morning Starr</a>
 */
@Slf4j
@RequiredArgsConstructor
public class CompletionServiceImpl implements CompletionService {

    private final OpenAiService openAiService;

    @Override
    public CompletionResponse create(CompletionCreateRequest createRequest) {
        String requestJson = GsonUtil.toJson(createRequest);
        String responseStr = openAiService.post(OpenAiApiUrl.Completions.CREATE.getUrl(openAiService.getApiHost()), requestJson);
        return GsonUtil.fromJson(responseStr, CompletionResponse.class);
    }
}
